package com.soupinabox;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;

/**
 * RegionFrame.java
 * <p/>
 * Author:  Taw
 * Date:    4/30/14
 */
public class RegionFrame extends JFrame {
    int posX=0,posY=0,lastX=0,lastY=0,lastW=0,lastH=0;
    boolean dt,db,dl,dr;
    static final int MARGIN = 12, MINHEIGHT=200,MINWIDTH=200*4/3;
    boolean dragleft=false,dragright=false,dragtop=false,dragbot=false;


    public Rectangle REGION = null;
    public RegionFrame() {
        super();

        setSize(300,200);
        setLocationRelativeTo(null);

        setAlwaysOnTop(true);


        addWindowFocusListener(new WindowFocusListener() {
            @Override
            public void windowGainedFocus(WindowEvent e) {

            }

            @Override
            public void windowLostFocus(WindowEvent e) {
                //System.out.println("HIDING");
                //setVisible(false);
            }
        });



        //Add a sample button.



        KeyListener kl=new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                //To change body of implemented methods use File | Settings | File Templates.
            }

            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    System.out.println("CONFIRM");
                    REGION = new Rectangle(getX(),getY(),getWidth(),getHeight());
                    System.out.println(REGION);
                    setVisible(false);
                } else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                    System.out.println("CANCEL");
                    setVisible(false);
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        };
        this.addKeyListener(kl);

        this.addMouseListener(new MouseAdapter()
        {
            public void mousePressed(MouseEvent e)
            {
                lastX = getX();
                lastY = getY();
                lastW = getWidth();
                lastH = getHeight();
                posX=e.getX();
                posY=e.getY();
                dragright = posX > getWidth()-10;
                dragbot = posY > getHeight()-10;
                dl=dr=dt=db=false;
                if(posX<MARGIN)dl=true;
                if(posX>=lastW-MARGIN)dr=true;
                if(posY<MARGIN)dt=true;
                if(posY>=lastH-MARGIN)db=true;
            }
        });

        this.addMouseMotionListener(new MouseAdapter()
        {
            public void mouseDragged(MouseEvent e)
            {
                //sets frame position when mouse dragged
                int ex = e.getXOnScreen();
                int ey = e.getYOnScreen();
                if(db||dr||dt||dl){
                    int t=(dt?ey-posY:lastY);
                    int l=(dl?ex-posX:lastX);
                    int r = (dr?lastW+ex-posX:lastX+lastW);
                    int b = (db?lastH+ey-posY:lastY+lastH);
                    if(r-l<MINWIDTH){
                        l+=(dl?r-l-MINWIDTH:0);
                        r=l+MINWIDTH;
                    }
                    if(b-t<MINHEIGHT){
                        t+=(dt?b-t-MINHEIGHT:0);
                        b=t+MINHEIGHT;
                    }
                    setLocation(l,t);
                    setSize(r-l,b-t);
                }else{
                    setLocation (e.getXOnScreen()-posX,e.getYOnScreen()-posY);
                }

            }
        });
        JPanel p=new MyPanel();
        p.addKeyListener(kl);
        add(p);
    }



    class MyPanel extends JPanel{

        public MyPanel(){
            super();
            setLayout(new GridBagLayout());

            GridBagConstraints c = new GridBagConstraints();
            c.gridx=c.gridy=0;
            //JButton b = new JButton("Click to confirm");
            add(new JLabel("Drag box to fit the game screen."),c);
            c.gridy++;
            add(new JLabel("Press Enter to confirm."),c);
            c.gridy++;
            add(new JLabel("Click outside or press Esc to cancel."),c);
        }

        protected void paintComponent(Graphics g){
            if(g instanceof Graphics2D){
                g.setColor(new Color(1,1,1,0.5f));
                g.fillRect(0, 0, getWidth(), getHeight());
                g.setColor(Color.BLACK);
                g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
                g.drawLine(0,MARGIN,getWidth(),MARGIN);
                g.drawLine(0,getHeight()-MARGIN-1,getWidth(),getHeight()-MARGIN-1);
                g.drawLine(MARGIN,0,MARGIN,getHeight()-1);
                g.drawLine(getWidth()-MARGIN-1,0,getWidth()-MARGIN-1,getHeight()-1);
            }
        }
    }
}
